package com.lx.mobileplayer.utils

import android.app.Activity
import android.content.Context
import android.content.Intent
import com.lx.mobileplayer.mvp.view.activity.MainActivity
import com.lx.mobileplayer.R
import com.lx.mobileplayer.mvp.view.activity.SettingActivity

/**
 * 类说明：activity跳转的工具类
 * Created by liuxiong on 2019/4/22.
 */
object AppManager {

    /** 保存activity的集合 */
    var activitys=ArrayList<Activity>()
    
    /**
    * 方法说明:  将activity加入管理
    * created by liuxiong on 2019/4/22 16:16
    */
    fun addActivity(activity: Activity){
        activitys.add(activity)
    }

    /**
     * 方法说明:  将activity移除管理
     * created by liuxiong on 2019/4/22 16:16
     */
    fun removeActivity(activity: Activity){
        activitys.forEach { 
            if(activity==it){
                activitys.remove(it)
            }
        }
    }
    
    /**
    * 方法说明:  获取最后一个activity
    * @return 
    * created by liuxiong on 16:20
    */
    fun getLastActivity():Activity?{
        return if (activitys.size >0) {
            activitys[activitys.size-1]
        } else {
            null
        }
    }


    /**
    * 方法说明:  activity跳转
    * @param  context 上下文（四大组件的上下文）
    * @param  intent 意图
    * @param  requestCode 请求码
    * created by liuxiong on 2019/4/22 16:03
    */
    fun  startActivity(context: Context, intent: Intent,requestCode:Int=0 ){


        if(context is Activity ){
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        }

        if(requestCode==0){
            context.startActivity(intent)
        }else{
            context as Activity
            context.startActivityForResult(intent,requestCode)

            //进入动画
            context.overridePendingTransition(R.anim.anim_activity_create_in,R.anim.anim_activity_create_out)
        }
    }

    /**
    * 方法说明:  跳转主界面
    * @param  context 上下文
    * created by liuxiong on 2019/4/22 16:07
    */
    fun goMainActivity(context: Context){
        startActivity(context,Intent(context, MainActivity::class.java))
    }
    /**
    * 方法说明:  跳转设置界面
    * @param  context 上下文
    * created by liuxiong on 2019/4/22 16:07
    */
    fun goSettingActivity(context: Context){
        startActivity(context,Intent(context, SettingActivity::class.java))
    }
}